package com.haoxi.gateway.test.message;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.haoxi.gateway.common.TestConfConst;
import com.haoxi.gateway.test.SGIPConstant;
import com.haoxi.gateway.test.util.TypeConvert;

public class SGIPActiveTestMessage extends SGIPMessage {
    private static SimpleDateFormat format = new SimpleDateFormat("MMddHHmmss");

    public SGIPActiveTestMessage() {
        int len = 20;
        buf = new byte[len];
        TypeConvert.int2byte(20, buf, 0);
        TypeConvert.int2byte(SGIPConstant.ACTIVE_TEST, buf, 4);
        int icpid = TestConfConst.icpid & 0xffffffff;
        TypeConvert.int2byte(icpid, buf, 8);
        int timeStamp = Integer.parseInt(format.format(new Date()));
        TypeConvert.int2byte(timeStamp, buf, 12);
        setSequenceID();
    }

    public SGIPActiveTestMessage(byte buf[]) throws IllegalArgumentException {
        super.buf = new byte[20];
        System.arraycopy(buf, 0, super.buf, 0, 20);
        message_Length = TypeConvert.byte2int(super.buf, 0);
        command_ID = TypeConvert.byte2int(super.buf, 4);
        src_node_Id = TypeConvert.byte2int(super.buf, 8);
        time_Stamp = TypeConvert.byte2int(super.buf, 12);
        sequence_Id = TypeConvert.byte2int(super.buf, 16);
    }

    public String toString() {
        String tmpStr = "SGIP_Active_Test: ";
        tmpStr = String.valueOf(String.valueOf((new StringBuffer(String
                .valueOf(String.valueOf(tmpStr)))).append("\r\nmsgLength=")
                .append(getallLength())));
        tmpStr = String.valueOf(String.valueOf((new StringBuffer(String
                .valueOf(String.valueOf(tmpStr)))).append("\r\nCommandId=")
                .append(getCommandId())));
        tmpStr = String.valueOf(String.valueOf((new StringBuffer(String
                .valueOf(String.valueOf(tmpStr)))).append("\r\nSrcNodeId=")
                .append(getSrcNodeId())));
        tmpStr = String.valueOf(String.valueOf((new StringBuffer(String
                .valueOf(String.valueOf(tmpStr)))).append("\r\nTime_Stamp=")
                .append(getTime_Stamp())));
        tmpStr = String.valueOf(String.valueOf((new StringBuffer(String
                .valueOf(String.valueOf(tmpStr)))).append("\r\nSequence_Id=")
                .append(getSequenceId())));
        return tmpStr;
    }

    public int getallLength() {
        return TypeConvert.byte2int(buf, 0);
    }

    public int getCommandId() {
        return TypeConvert.byte2int(buf, 4);
    }

    public int getSrcNodeId() {
        return TypeConvert.byte2int(buf, 8);
    }

    public int getTime_Stamp() {
        return TypeConvert.byte2int(buf, 12);
    }

    public int getSequenceId() {
        return TypeConvert.byte2int(buf, 16);
    }

    public String getSeqStr() {
        return "" + getSrcNodeId() + getTime_Stamp() + getSequenceId();
    }

}
